import { getOrgList, getRoleList } from '@/api/identity';
import { messageBusinessDicts, messageGradeDicts } from '@/api/message'

const state = {
    orgList: [],
    roleList: [],
    messageGrade: [],
    messageBusiness: [],
}

const mutations = {
    SET_ORG_LIST: (state, orgList) => {
        state.orgList = orgList
    },
    SET_ROLE_LIST: (state, roleList) => {
        state.roleList = roleList
    },
    SET_MESSAGE_BUSINESS_LIST: (state, messageBusiness) => {
        state.messageBusiness = messageBusiness
    },
    SET_MESSAGE_GRADE_LIST: (state, messageGrade) => {
        state.messageGrade = messageGrade
    }
}

const actions = {
    getOrgList({ commit }, { params, refresh }) {
        if (state.orgList.length > 0 && !refresh) {
            return new Promise(resolve => resolve(state.orgList))
        } else {
            return new Promise((resolve, reject) => {
                getOrgList(params.id).then(res => {
                    if (!res.error) {
                        commit('SET_ORG_LIST', res.children)
                        resolve(res.children)
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        }
    },
    getRoleList({ commit }, { params, refresh }) {
        if (state.roleList.length > 0 && !refresh) {
            return new Promise(resolve => resolve(state.roleList))
        } else {
            return new Promise((resolve, reject) => {
                getRoleList(params).then(res => {
                    if (!res.error) {
                        commit('SET_ROLE_LIST', res.items)
                        resolve(res.items)
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        }
    },
    dictMessageBusiness({ commit }, refresh) {
        if (state.messageBusiness.length > 0 && !refresh) {
            return new Promise(resolve => resolve(state.messageBusiness))
        } else {
            return new Promise((resolve, reject) => {
                messageBusinessDicts().then(res => {
                    if (!res.error) {
                        commit('SET_MESSAGE_BUSINESS_LIST', res.data)
                        resolve(res.data)
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        }
    },
    dictMessageGrade({ commit }, refresh) {
        if (state.messageGrade.length > 0 && !refresh) {
            return new Promise(resolve => resolve(state.messageGrade))
        } else {
            return new Promise((resolve, reject) => {
                messageGradeDicts().then(res => {
                    if (!res.error) {
                        commit('SET_MESSAGE_GRADE_LIST', res.data)
                        resolve(res.data)
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        }
    },
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
